
#运行nano的设备如D1S 6ULL 查看 riscv64-unknown-linux-gnu-gcc -v 查看BUILD HOST
HOST=riscv64-unknown-linux-gnu
#编译的平台x86 linux 通过gcc -v 查看 BUILD/TARGET
BUILD=x86_64-linux-gnu
#安装目录
PREFIX=/opt/nano

NCURSE_PREFIX=/opt/ncurse

CROSS_COMPILE_PREFIX=/opt/riscv64-glibc-gcc-thead_20200702/bin/riscv64-unknown-linux-gnu-

NANO_VER=7.1
NCURSE_VER=6.4

NANO_COMPRESS_PACKAGE=nano-${NANO_VER}.tar.gz
NANO_PACKAGE_NAME=nano-${NANO_VER}

ncurses_COMPRESS_PACKAGE=ncurses-${NCURSE_VER}.tar.gz
ncurses_PACKAGE_NAME=ncurses-${NCURSE_VER}
#下载源码包
do_download_nano_src () {
  echo  -e "\033[1;33m start download ${NANO_COMPRESS_PACKAGE}...\033[0m"
  
  cd ..
 
  if [ ! -f "${NANO_PACKAGE_NAME}" ];then
     if [ ! -d "${NANO_PACKAGE_NAME}" ];then
        wget -c https://mirrors.ustc.edu.cn/gnu/nano/nano-${NANO_VER}.tar.gz
     fi
  fi

  echo  -e  "\033[1;33m nano download done...\033[0m" 
}

do_download_ncurses_src () {
  echo  -e "\033[1;33m start download ${NANO_COMPRESS_PACKAGE}...\033[0m"

  if [ ! -f "${ncurses_COMPRESS_PACKAGE}" ];then
     if [ ! -d "${ncurses_PACKAGE_NAME}" ];then
        wget -c https://mirrors.ustc.edu.cn/gnu/ncurses/ncurses-${NCURSE_VER}.tar.gz
     fi
  fi

  echo  -e "\033[1;33m ncurses download done...\033[0m" 
}

#解压源码包
do_tar_nano_package () {
  echo -e "\033[1;33mstart unpacking the ${NANO_COMPRESS_PACKAGE} package ...\033[0m"
  if [ ! -d "${NANO_PACKAGE_NAME}" ];then
     tar -xf ${NANO_COMPRESS_PACKAGE}
  fi
  echo -e "\033[1;33mdone...\033[0m"
  #cd ${PACKAGE_NAME} 
}

#解压源码包
do_tar_ncurse_package () {
  echo -e "\033[1;33mstart unpacking the ${ncurses_COMPRESS_PACKAGE} package ...\033[0m"
  if [ ! -d "${ncurses_PACKAGE_NAME}" ];then
     tar -xf ${ncurses_COMPRESS_PACKAGE}
  fi
  echo -e "\033[1;33mdone...\033[0m"
  #cd ${PACKAGE_NAME} 
}


#配置选项
do_ncurse_configure () {
  echo -e "\033[1;33mstart configure ${ncurses_PACKAGE_NAME}...\033[0m"

export CC=${CROSS_COMPILE_PREFIX}gcc
export CXX=${CROSS_COMPILE_PREFIX}g++
 
 mkdir _ncurse
 cd _ncurse
 ../${ncurses_PACKAGE_NAME}/configure \
 --prefix=${NCURSE_PREFIX} \
 --build=${BUILD} \
 --host=${HOST} \
 --without-cxx \
 --without-cxx-binding \
 --without-ada \
 --without-manpages \
 --without-progs \
 --without-tests \
 --with-shared

  echo -e "\033[1;33mdone...\033[0m"
} 


#配置选项
do_nano_configure () {
  echo -e "\033[1;33mstart configure ${NANO_PACKAGE_NAME}...\033[0m" 

export CC=${CROSS_COMPILE_PREFIX}gcc
export CXX=${CROSS_COMPILE_PREFIX}g++

export CFLAGS="-I${NCURSE_PREFIX}/include/ncurses -I${NCURSE_PREFIX}/include"
export LDFLAGS="-L${NCURSE_PREFIX}/lib" 
 
 mkdir _nano
 cd _nano
 ../${NANO_PACKAGE_NAME}/configure \
 --prefix=${PREFIX} \
 --build=${BUILD} \
 --host=${HOST}
  echo -e "\033[1;33m nano configrue done...\033[0m"
} 

#编译并且安装
do_make_ncurse_install () {
  echo -e "\033[1;33m start make and install ${ncurses_PACKAGE_NAME} ... \033[0m"
  make -j6 && make install
  echo -e "\033[1;33mdone...\033[0m"
}
 

 #编译并且安装
do_make_nano_install () {
  echo  -e  "\033[1;33mstart make and install ${NANO_PACKAGE_NAME} ...\033[0m"
  make -j6 && make install
  echo  -e  "\033[1;33m 复制libncurses.so \033[0m"
  cp ${NCURSE_PREFIX}/lib/libncurses.so.6 ${PREFIX}/bin
  echo  -e  "\033[1;33mdone...\033[0m"
}
 

#ncurse 
do_download_ncurses_src
do_tar_ncurse_package
do_ncurse_configure
do_make_ncurse_install

#nano
do_download_nano_src
do_tar_nano_package
do_nano_configure
do_make_nano_install


 
